using RabbitMQ.Client;

namespace Beauty.Net.RabbitMQ.SimpleQueue
{
    /// <summary>
    /// 简单队列生产者
    /// </summary>
    public class SimpleQueueProducer : RabbitMQProducerBase
    {
        public SimpleQueueProducer(IRabbitMQConnectionManager connectionManager)
            : base(connectionManager)
        {}

        /// <summary>
        /// 发送消息到简单队列
        /// </summary>
        public void Send<T>(string queueName, T message, bool persistent = true)
        {
            using (var channel = _connectionManager.CreateChannel())
            {
                // 声明队列
                channel.QueueDeclare(queue:
queueName,
                                    durable: persistent,
                                    exclusive: false,
                                    autoDelete: false,
                                    arguments: null);

                var properties = channel.CreateBasicProperties();
                properties.Persistent = persistent;

                var messageBody = System.Text.Encoding.UTF8.GetBytes(System.Text.Json.JsonSerializer.Serialize(message, _jsonOptions));
                channel.BasicPublish(exchange: string.Empty, routingKey: queueName, basicProperties: properties, body: messageBody);
            }
        }
    }
}